package com.doucome.snapshot.view
{
	import com.doucome.snapshot.Count;
	import com.doucome.snapshot.Global;
	import com.doucome.snapshot.Local;
	import com.doucome.snapshot.User;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.net.sendToURL;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.text.TextField;
	import flash.display.Stage;
	import flash.events.IOErrorEvent;
	
	/**
	 * ...
	 * @author wkl
	 */
	public class LinkView extends Sprite
	{
		public static const LINK_VIEW_RETAKEPIC_EVENT:String = "LinkViewReTakePic";
		public static const LINK_VIEW_EDIT_EVENT:String = "LinkViewEditPic";
		private static const V_W:int = 760;
		private static const V_H:int = 580;
		private var _photo:Photo;
		private var _find_photo:Photo;
		private var _photoBmd:BitmapData;
		private var _btn_link:BtnLink;
		private var _btn_re_take_pic:BtnReTakePic;
		private var _pingfenview:PingFenView;
		private var _btn_next_link:BtnNextLink;
		private var _btn_retake:BtnReTake;
		private var _jd_effect:JdEffect;
		private var _fen_view:FenView;
		private var link_count:int = 0;
		private var _window_error:WindowError;
		private var _waitEffect:WaitEffect;
		private var _mask:Sprite;
		private var user:User;
		private var reload:Boolean = false;
		
		
		public function LinkView()
		{
			//_xz初始值
			//_xz = Global.bg.xz;
			//photo
			_photo = new Photo(0);
			_photo.x = 74;
			_photo.y = 153;
			_photoBmd = new BitmapData(400, 400, true);
			_photo.addEventListener(Photo.PIC_RETAKE_EVENT, onEditPic);
			addChild(_photo);
			
			//find_photo
			_find_photo = new Photo(1);
			_find_photo.x = 452.5;
			_find_photo.y = 152.929;
			addChild(_find_photo);
			
			//btn_re_take
			_btn_retake = new BtnReTake();
			_btn_retake.addEventListener(MouseEvent.CLICK, onReTakePic);
			_btn_retake.x = 344.464;
			_btn_retake.y = 255.607;
			
			addChild(_btn_retake);
			
			//btn_link
			_btn_link = new BtnLink();
			_btn_link.x = 344.464;
			_btn_link.y = 255.607;
			addChild(_btn_link);
			_btn_link.addEventListener(MouseEvent.CLICK, onLink);
			
			//pingfenview
			_pingfenview = new PingFenView();
			_pingfenview.x = 246;
			_pingfenview.y = 478;
			_pingfenview.visible = false;
			_pingfenview.addEventListener(Event.OPEN, wait);
			_pingfenview.addEventListener(PingFenView.PING_FEN_FINISH_EVENT, onPingfenFinish);
			_pingfenview.addEventListener(IOErrorEvent.IO_ERROR, onPingfenError);
			addChild(_pingfenview);
			
			//_btn_next_link
			_btn_next_link = new BtnNextLink();
			_btn_next_link.x = 344.464;
			_btn_next_link.y = 255.607;
			_btn_next_link.visible = false;
			_btn_next_link.addEventListener(MouseEvent.CLICK, onNextLink);
			addChild(_btn_next_link);
			
			//jd_effect
			_jd_effect = new JdEffect();
			_jd_effect.x = 645;
			_jd_effect.y = 174;
			_jd_effect.visible = false;
			addChild(_jd_effect);
			
			//_fen_view
			_fen_view = new FenView();
			_fen_view.reset();
			_fen_view.x = _find_photo.x + 20;
			_fen_view.y = _find_photo.y + 40;
			addChild(_fen_view);
			
			//_window_error
			_window_error = new WindowError();
			_window_error.visible = false;
			addChild(_window_error);
			
			//_wait_effect
			_waitEffect = new WaitEffect;
			addChild(_waitEffect);
			_waitEffect.close();
		
		}
		
		private function onEditPic(e:Event):void 
		{
			this.close();
			dispatchEvent(new Event(LINK_VIEW_EDIT_EVENT));
		}
		
		private function onPingfenError(e:IOErrorEvent):void
		{
			wait_off();
			_window_error.open(Local.ERROR_TXT);
		}
		
		private function onNextLink(e:MouseEvent):void
		{
			if (link_count < 5)
			{
				wait(e);
				_btn_next_link.visible = false;
				_jd_effect.visible = false;
				_fen_view.reset();
				_pingfenview.turnOff();
				_find_photo.addEventListener(Photo.PIC_LOADED_EVENT, onPicLoaded);
				_find_photo.addEventListener(Photo.PIC_LOAD_FAIL_EVENT, onPicLoadFail);
				_find_photo.reset();
				_find_photo.loadPic();
			}
			else
			{
				_window_error.open(Local.LINK_TIMES_LIMIT_TXT);
				_btn_retake.visible = true;
				_btn_next_link.visible = false;
				
			}
		
			//_pingfenview.visible = true;
		}
		
		private function onPingfenFinish(e:Event):void
		{
			wait_off();
			_jd_effect.visible = true;
			_jd_effect.play();
			_pingfenview.turnOff();
			_btn_next_link.visible = true;
			_fen_view.fen = _pingfenview._fen / 20;
		
			//trace("fen" + _fen_view.fen);
		}
		
		private function initPhoto():void
		{
			if (!contains(_photo))
			{
				
			}
		
		}
		
		private function initFindPhoto():void
		{
			if (!contains(_find_photo))
			{
				
			}
		}
		
		private function initBtnLink():void
		{
			if (!contains(_btn_link))
			{
				
			}
		
		}
		
		private function onLink(e:MouseEvent):void
		{
			
			_btn_link.visible = false;
			wait(e);
			_photo.addEventListener(Photo.PIC_UPLOADED_EVENT, onPicUpLoaded);
			_photo.addEventListener(Photo.PIC_UPLOAD_FAIL_EVENT, onPicUpLoadFail);
			_photo.upload();
		
		}
		
		private function wait(e:Event):void
		{
			_waitEffect.show();
		}
		
		private function wait_off():void
		{
			_waitEffect.close();
		}
		
		private function onPicUpLoadFail(e:Event):void
		{
			wait_off();
			_btn_link.visible = true;
			_window_error.open(Local.ERROR_TXT);
			_photo.removeEventListener(Photo.PIC_UPLOAD_FAIL_EVENT, onPicUpLoadFail);
		}
		
		//图片上传成功开始link
		private function onPicUpLoaded(e:Event):void
		{
			_photo.removeEventListener(Photo.PIC_UPLOADED_EVENT, wait);
			_photo.removeEventListener(Photo.PIC_UPLOADED_EVENT, onPicUpLoaded);
			_photo.removeEventListener(Photo.PIC_UPLOAD_FAIL_EVENT, onPicUpLoadFail);
			_find_photo.addEventListener(Photo.PIC_LOADED_EVENT, onPicLoaded);
			_find_photo.addEventListener(Photo.PIC_LOAD_FAIL_EVENT, onPicLoadFail);
			_find_photo.loadPic();
		}
		
		private function onPicLoadFail(e:Event):void
		{
			wait_off();
			_window_error.open(Local.ERROR_TXT);
			_find_photo.addEventListener(Photo.PIC_LOAD_FAIL_EVENT, onPicLoadFail);
			_btn_next_link.visible = true;
			reload = true;
		
		}
		
		private function onPicLoaded(e:Event):void
		{
			
			wait_off();
			if (reload)
			{
				reload = false;
			}
			else
			{
				
				if (link_count == 0)
				{
					
					Global.bg.xz -= 1;
					updateCout(Count.COUNT_XZ, 1);
				}
				link_count++;
				Global.bg.times++;
				updateCout(Count.COUNT_TIMES, 1);
				
			}
			
			_find_photo.removeEventListener(Photo.PIC_LOADED_EVENT, onPicLoaded);
			_pingfenview.turnON(_find_photo.photoid, _find_photo.photo_userid, Global.user.id);
		
		}
		
		private function onReTakePic(e:Event):void
		{
			
			this.close();
			dispatchEvent(new Event(LINK_VIEW_RETAKEPIC_EVENT));
		
		}
		
		public function get photoBmd():BitmapData
		{
			return _photoBmd;
		}
		
		public function set photoBmd(value:BitmapData):void
		{
			if (value !== null)
			{
				_photoBmd.dispose();
				_photoBmd = value;
			}
		
		}
		
		
		
		public function show():void
		{
			
			_btn_link.visible = true;
			_btn_retake.visible = false;
			_find_photo.reset();
			_pingfenview.turnOff();
			_btn_next_link.visible = false;
			_jd_effect.visible = false;
			_photo.showPic(_photoBmd);
			
		}
		
		public function close():void
		{
			_pingfenview.visible = false;
			_pingfenview.turnOff();
			_fen_view.reset();
			_find_photo.reset();
			_photo.reset();
			link_count = 0;
			_window_error.visible = false;
			this.visible = false;
		}
		
		private function showPingFenView():void
		{
			_pingfenview.turnON(_find_photo.photoid, _find_photo.photo_userid, Global.user.id);
		}
		
		private function updateCout(type:int, value:int):void
		{
			var rq = new URLRequest("http://" + Global.APP_SERVER + "/snapshot/index.php");
			var vars:URLVariables = new URLVariables("a=upcount&type=" + type + "&userid=" + Global.user.id + "&value=" + value);
			rq.data = vars;
			sendToURL(rq);
		}
	
	}

}